<?php
namespace App\Http\Controllers\Job;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;
class LoginController extends \App\Http\Controllers\JobController {
    /**
     * 登录
     */
    public function index(Request $request) {
        if (Session::has('job')) {
            return redirect('job/index/index');
        }
        if ($request->has('uphone')) {
            $res = \App\Models\InstPerson::where('phone', $request->input('uphone'))->first();
            if ($res) {
                Session::put('job', $res);
                Session::save();
                return redirect('job/index/index');
            }
        }
        if ($request->ajax()) {
            $res = \App\Models\InstPerson::where('phone', $request->input('phone'))->first();
            if ($res) {
                $msg = $this->checkPassword($res, $request->input('password'));
            } else {
                $msg = ['status'=>0, 'info'=>'用户不存在'];
            }
            return \Illuminate\Support\Facades\Response::json($msg);
        }
        return view('job.login.index');
    }
    
    private function checkPassword($res, $password) {
        if ($res->password == md5($password)) {
            Session::put('job', $res);
            Session::save();
            return ['status'=> 1, 'info'=>'登录成功'];
        } else {
            return ['status'=>0, 'info'=>'密码错误'];
        }
    }
    /**
     * 用户退出
     * @param Request $request
     * @return type
     */
    public function logout(Request $request) {
        Session::forget('job');
        return redirect('job/login/index');
    }
}
